/**
 * 封装网络请求
 * GET POST DELET PUT UPLOAD
 *
 * 注意，在微信小程序中使用DELETE请求时，params参数不会正常识别，所以需要直接写在URL中
 */

import { BASE_REQUEST_URL, REQUEST_ERROR_CODE } from './config/ServicesConfig';

/**
 * 封装获取请求
 * @param {string} url - 传递过来的请求地址
 * @param {object} params - 传递过来的请求数据
 *
 * @returns {Promise} 返回请求结果的 Promise
 */
export const getHttp = (url: string, params: object): Promise<any> => {
	return new Promise((resolve: Function, reject: Function) => {
		uni.request({
			url: BASE_REQUEST_URL + url,
			data: params,
			method: 'GET',
			header: {
				'content-type': 'application/json', // 默认值
				Authorization: uni.getStorageSync('Token') ? uni.getStorageSync('Token') : ''
			},
			success: (result: any) => {
				if (result.data.code === REQUEST_ERROR_CODE.INVALID_TOKEN) {
					// 登录失败，请重新登录
					uni.showToast({
						title: result.data.msg,
						icon: 'none'
					});
					uni.clearStorageSync();
					uni.reLaunch({
						url: '/pages/index/index'
					});
				} else {
					// 请求成功，就将成功的数据返回出去
					resolve(result.data);
				}
			},
			fail: (err: any) => {
				reject(err);
			}
		});
	});
};

/**
 * 封装发送请求
 * @param {string} url - 传递过来的请求地址
 * @param {object} data - 传递过来的发送数据
 *
 * @returns {Promise} 返回请求结果的 Promise
 */
export const postHttp = (url: string, data: object): Promise<any> => {
	return new Promise((resolve: Function, reject: Function) => {
		uni.request({
			url: BASE_REQUEST_URL + url,
			data: data,
			method: 'POST',
			header: {
				'content-type': 'application/json', // 默认值
				Authorization: uni.getStorageSync('Token') ? uni.getStorageSync('Token') : ''
			},
			success: (result: any) => {
				if (result.data.code === REQUEST_ERROR_CODE.INVALID_TOKEN) {
					// 登录失败，请重新登录
					uni.showToast({
						title: result.data.msg,
						icon: 'none'
					});
					uni.clearStorageSync();
					uni.reLaunch({
						url: '/pages/index/index'
					});
				} else {
					// 请求成功，就将成功的数据返回出去
					resolve(result.data);
				}
			},
			fail: (err: any) => {
				reject(err);
			}
		});
	});
};

/**
 * 封装修改请求
 * @param {string} url - 传递过来的请求地址
 * @param {object} data - 传递过来的修改数据
 *
 * @returns {Promise} 返回请求结果的 Promise
 */
export const putHttp = (url: string, data: object): Promise<any> => {
	return new Promise((resolve: Function, reject: Function) => {
		uni.request({
			url: BASE_REQUEST_URL + url,
			data: data,
			method: 'PUT',
			header: {
				'content-type': 'application/json', // 默认值
				Authorization: uni.getStorageSync('Token') ? uni.getStorageSync('Token') : ''
			},
			success: (result: any) => {
				if (result.data.code === REQUEST_ERROR_CODE.INVALID_TOKEN) {
					// 登录失败，请重新登录
					uni.showToast({
						title: result.data.msg,
						icon: 'none'
					});
					uni.clearStorageSync();
					uni.reLaunch({
						url: '/pages/index/index'
					});
				} else {
					// 请求成功，就将成功的数据返回出去
					resolve(result.data);
				}
			},
			fail: (err: any) => {
				reject(err);
			}
		});
	});
};

/**
 * 封装删除请求
 * @param {string} url - 传递过来的请求地址
 * @param {object} params - 传递过来的删除数据
 *
 * @returns {Promise} 返回请求结果的 Promise
 */
export const deleteHttp = (url: string, params: object): Promise<any> => {
	const queryString = new URLSearchParams(Object.entries(params).map(([key, value]) => [key, String(value)])).toString();
	return new Promise((resolve: Function, reject: Function) => {
		uni.request({
			url: BASE_REQUEST_URL + url + `?${queryString}`,
			data: params,
			method: 'DELETE',
			header: {
				'content-type': 'application/json', // 默认值
				Authorization: uni.getStorageSync('Token') ? uni.getStorageSync('Token') : ''
			},
			success: (result: any) => {
				if (result.data.code === REQUEST_ERROR_CODE.INVALID_TOKEN) {
					// 登录失败，请重新登录
					uni.showToast({
						title: result.data.msg,
						icon: 'none'
					});
					uni.clearStorageSync();
					uni.reLaunch({
						url: '/pages/index/index'
					});
				} else {
					// 请求成功，就将成功的数据返回出去
					resolve(result.data);
				}
			},
			fail: (err: any) => {
				reject(err);
			}
		});
	});
};

/**
 * 封装上传请求
 * @param {string} url - 传递过来的请求地址
 * @param {*} filePath - 传递过来的上传数据
 *
 * @returns {Promise} 返回请求结果的 Promise
 */
export const uploadeHttp = (url: string, filePath: any): Promise<any> => {
	return new Promise((resolve: Function, reject: Function) => {
		uni.uploadFile({
			url: BASE_REQUEST_URL + url,
			filePath: filePath,
			name: 'file',
			header: {
				'content-type': 'application/json', // 默认值
				Authorization: uni.getStorageSync('Token') ? uni.getStorageSync('Token') : ''
			},
			success: (result: any) => {
				let rquestData = JSON.parse(result.data);
				if (rquestData.code === REQUEST_ERROR_CODE.INVALID_TOKEN) {
					// 登录失败，请重新登录
					uni.showToast({
						title: rquestData.msg,
						icon: 'none'
					});
					uni.clearStorageSync();
					uni.reLaunch({
						url: '/pages/index/index'
					});
				} else {
					// 请求成功，就将成功的数据返回出去
					resolve(rquestData);
				}
			},
			fail: (err: any) => {
				reject(err);
			}
		});
	});
};
